import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@WebServlet(name = "FileDownServlet",urlPatterns = "/downfile")
public class FileDownServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String("测试文档8899.txt".getBytes(), "ISO-8859-1"));
        ServletContext context=this.getServletContext();
        String path=context.getRealPath("/WEB-INF/it.properties");

        InputStream in=new FileInputStream(path);
        OutputStream out=response.getOutputStream();

        int b=0;
        while (true){
            b=in.read();
            out.write(b);
            if (b==-1){
                break;
            }
            System.out.println(b);
        }
        in.close();
        out.close();

    }
}
